package Model;

import java.io.*;
import java.util.Date;

import javax.swing.JDialog;
import javax.swing.JLabel;

public final class Contact { //If no inheritance : make it final
	
	public String firstName;
	public String lastName;
	public String homeAddress;
	public String companyAddress;
	public String personalMobilePhone;
	public String personalHomePhone;
	public String proMobilePhone;
	public String proOfficePhone;
	public String homePage;
	public String proEmail;
	public String personalEmail;
	public String otherIM;
	public String otherSkype;
	public String dateAdded;
	public String group;
	public String picturePath;

	public Contact() {
		firstName = new String();
		lastName = new String();
		homeAddress = new String();
		companyAddress = new String();
		personalMobilePhone = new String();
		personalHomePhone = new String();
		proMobilePhone = new String();
		proOfficePhone = new String();
		homePage = new String();
		proEmail = new String();
		personalEmail = new String();
		otherIM = new String();
		otherSkype = new String();
		group = new String();
		picturePath = new String();
	}
	
	public void saveContact() {
		Date d = new Date();
		dateAdded = d.toString();
		File file;
		file = new File("contacts.csv");
		FileInputStream fileInput;
		try {
			String write = new String();
			write = firstName + ";" + lastName + ";" 
			+ homeAddress + ";" + companyAddress + ";" 
					+ personalMobilePhone + ";" + personalHomePhone + ";"
					+ proMobilePhone + ";" + proOfficePhone + ";"
				    + homePage + ";" + proEmail + ";" + personalEmail + ";"
				    + otherIM + ";" + otherSkype + ";" + dateAdded + ";" 
				    + group + ";" + picturePath + ";";
			String personInfo = new String();
			personInfo = firstName + ";" + lastName + ";" 
					+ homeAddress + ";" + companyAddress + ";" 
							+ personalMobilePhone + ";" + personalHomePhone + ";"
							+ proMobilePhone + ";" + proOfficePhone + ";"
						    + homePage + ";" + proEmail + ";" + personalEmail + ";"
						    + otherIM + ";" + otherSkype + ";";
			System.out.println(write);
			fileInput = new FileInputStream("contacts.csv");
			BufferedReader br = new BufferedReader(new InputStreamReader(new DataInputStream(fileInput)));
			String read = new String();
			read = br.readLine(); //Skip first line
			boolean alreadyContact = false;
			while ((read = br.readLine()) != null)
        	{
        		if (read.contains(personInfo))
        		{
        			if (read.contains(picturePath))
        			{
	        			JDialog popup = new JDialog();
	        			popup.add(new JLabel("Contact Already exists"));
	        			popup.setSize(200,100);
	        			popup.setLocationRelativeTo(null);
	        			popup.setVisible(true);
	        			alreadyContact = true;
        			}
        		}
        	}
			br.close();
			fileInput.close();
			
			if (!alreadyContact)
			{
        	PrintWriter printerOut = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
			printerOut.println(write);
			printerOut.close();
			}
			
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
	}

	public void delete(String contactDeleteDateAdded) {
		System.out.println(contactDeleteDateAdded);
		File fileInput = new File("contacts.csv");
		File tempFile = new File("contactsTemp.csv");
		try {
			BufferedReader br = new BufferedReader(new FileReader(fileInput));
			BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile));
			PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
			String read = new String();
			read = br.readLine(); //Skip first line
			pw.println(read);
			while ((read = br.readLine()) != null)
	    	{
	    		if (!read.contains(contactDeleteDateAdded))
	    		{
	    			pw.println(read);
	    			pw.flush();
	    		}
	    		else System.out.println("found");
	    	}
			br.close();
			bw.close();
			pw.close();
			tempFile.renameTo(fileInput);
			System.out.println("deleted");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void modify(String contactModifyDateAdded)
	{
		
		delete(contactModifyDateAdded);
		
		saveContact();
	}
	
	
	
}
